/*
 * Debts Manager. Simple android application for managing debts.
 * Copyright (C) 2010 Maxim Manuylov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
 */

package manuylov.maxim.dm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import manuylov.maxim.common.util.IntentUtil;
import manuylov.maxim.dm.Constants;
import manuylov.maxim.dm.R;

public class Main extends BaseDMActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView appNameVersionTextView = (TextView) findViewById(R.id.app_name_version_text);
        final StringBuilder builder = new StringBuilder("");
        builder.append(getString(R.string.app_name));
        builder.append(" ");
        builder.append(Constants.VERSION);
        appNameVersionTextView.setText(builder.toString());

        final Button lendButton = (Button) findViewById(R.id.lend_button);
        lendButton.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                final Intent intent = prepareNewRecordIntent(Constants.LEND);
                intent.putExtra(Constants.ACTIVITY_LABEL_TEXT, R.string.lend);
                intent.putExtra(Constants.TO_FROM_LABEL_TEXT, R.string.to);
                startActivityForResult(intent, Constants.REQUEST_DEFAULT);
            }
        });

        final Button borrowButton = (Button) findViewById(R.id.borrow_button);
        borrowButton.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                final Intent intent = prepareNewRecordIntent(Constants.BORROW);
                intent.putExtra(Constants.ACTIVITY_LABEL_TEXT, R.string.borrow);
                intent.putExtra(Constants.TO_FROM_LABEL_TEXT, R.string.from);
                startActivityForResult(intent, Constants.REQUEST_DEFAULT);
            }
        });

        final Button balanceButton = (Button) findViewById(R.id.balance_button);
        balanceButton.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                final Intent intent = prepareViewBalanceIntent(Constants.BALANCE_MODE_ACTUAL);
                intent.putExtra(Constants.ACTIVITY_LABEL_TEXT, R.string.balance);
                startActivityForResult(intent, Constants.REQUEST_DEFAULT);
            }
        });

        final Button historyButton = (Button) findViewById(R.id.history_button);
        historyButton.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                final Intent intent = prepareViewBalanceIntent(Constants.BALANCE_MODE_HISTORY);
                intent.putExtra(Constants.ACTIVITY_LABEL_TEXT, R.string.history_label);
                startActivityForResult(intent, Constants.REQUEST_DEFAULT);
            }
        });
    }

    @Override
    public boolean onPrepareOptionsMenu(final Menu menu) {
        menu.findItem(R.id.home_menu_item).setVisible(false);
        menu.findItem(R.id.settings_menu_item).setVisible(true);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void goToHome() {
        // Do nothing
    }

    private Intent prepareViewBalanceIntent(final int balanceMode) {
        final Intent intent = IntentUtil.prepareExplicitIntent(Main.this, Constants.ACTION_VIEW_GROUPED_BALANCE, GroupedBalance.class);
        intent.putExtra(Constants.BALANCE_MODE, balanceMode);
        return intent;
    }

    private Intent prepareNewRecordIntent(final int recordType) {
        final Intent intent = IntentUtil.prepareExplicitIntent(this, Constants.ACTION_NEW_RECORD, NewRecord.class);
        intent.putExtra(Constants.NEW_RECORD_TYPE, recordType);
        return intent;
    }
}